/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpInventoryCheck;
import com.jims.master.modules.csm.entity.ExpStock;
import com.jims.master.modules.csm.service.ExpInventoryCheckService;
import com.jims.master.modules.csm.service.ExpStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消耗品盘点Controller
 * @author zhuq
 * @version 2017-03-28
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expInventoryCheck")
public class ExpInventoryCheckController extends BaseController {

	@Autowired
    private ExpInventoryCheckService expInventoryCheckService;
    @Autowired
    private ExpStockService expStockService;

	@ModelAttribute
	public ExpInventoryCheck get(@RequestParam(required=false) String id) {
		ExpInventoryCheck entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expInventoryCheckService.get(id);
		}
		if (entity == null){
			entity = new ExpInventoryCheck();
		}
		return entity;
	}

    /**
     * 消耗品盘点首页
     * @param expInventoryCheck
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ExpInventoryCheck expInventoryCheck, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpStock> page = expStockService.findInventoryStockPage(new Page<ExpStock>(request, response), expInventoryCheck);
        model.addAttribute("page", page);
		return "modules/csm/expInventoryCheckIndex";
	}



	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ExpInventoryCheck expInventoryCheck, Model model) {
		model.addAttribute("expInventoryCheck", expInventoryCheck);
		return "modules/csm/expInventoryCheckForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExpInventoryCheck expInventoryCheck, Model model) {
	    StringData data=new StringData();
		int i=expInventoryCheckService.save(expInventoryCheck);
        if(i>0){
            data.setCode("success");
            data.setData("保存消耗品盘点成功");
        }else{
            data.setCode("error");
            data.setData("保存消耗品盘点失败");
        }
        return data;
	}

    /*
    * 消耗品盘点，将一个消耗品不同批次的盘点数据保存
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveList")
    @ResponseBody
    public StringData saveList(@RequestBody List<ExpInventoryCheck> ExpInventoryCheckList,HttpServletRequest request, HttpServletResponse response) {
        StringData data=new StringData();
        int i=expInventoryCheckService.saveList(ExpInventoryCheckList);
        return data;
    }

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExpInventoryCheck expInventoryCheck) {
	    StringData data=new StringData();
		int i=expInventoryCheckService.delete(expInventoryCheck);
        if(i>0){
            data.setCode("success");
            data.setData("删除消耗品盘点成功");
        }else{
            data.setCode("error");
            data.setData("删除消耗品盘点失败");
        }
		return data;
	}


	/**
	 * 消耗品盘点统计
	 * @param expInventoryCheck
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author CTQ
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"recordIndex", ""})
	public String recordIndex(ExpInventoryCheck expInventoryCheck, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpStock> page = expStockService.findRecordPage(new Page<ExpStock>(request, response), expInventoryCheck);
		model.addAttribute("page", page);
		return "modules/csm/expInventoryCheckRecordIndex";
	}

	/**
	 * 查询药品盘点记录明细
	 * @param expInventoryCheck
	 * @param model
	 * @return
	 * @author CTQ
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "recordDetail")
	public String recordDetail(ExpInventoryCheck expInventoryCheck, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpInventoryCheck> page = expInventoryCheckService.findPage(new Page<ExpInventoryCheck>(request, response), expInventoryCheck);
		model.addAttribute("page", page);
		return "modules/csm/expInventoryCheckRecordDetail";
	}

	/**
	 * 药品盘点盈亏统计
	 * @param expInventoryCheck
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"profitLossIndex", ""})
	public String profitLossIndex(ExpInventoryCheck expInventoryCheck, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpInventoryCheck> page = expInventoryCheckService.findProfigLossPage(new Page<ExpInventoryCheck>(request, response), expInventoryCheck);
		model.addAttribute("page", page);
		return "modules/csm/expInventoryCheckProfitLossIndex";
	}

}